import tensorflow as tf

a = tf.constant(1.0)
b = tf.constant(2.0)

c = tf.add(a, b)
print(a)
print(b)
print(c)
"""
Tensor("Const:0", shape=(), dtype=float32)
Tensor("Const_1:0", shape=(), dtype=float32)
Tensor("Add:0", shape=(), dtype=float32)
"""
print('*' * 50)

# 获取默认图
g = tf.compat.v1.get_default_graph()

print(a.graph)
print(b.graph)
print(c.graph)
print(g)
print('*' * 50)
"""
<tensorflow.python.framework.ops.Graph object at 0x0000024B948834C8>
<tensorflow.python.framework.ops.Graph object at 0x0000024B948834C8>
<tensorflow.python.framework.ops.Graph object at 0x0000024B948834C8>
<tensorflow.python.framework.ops.Graph object at 0x0000024B948834C8>
"""

# 自定义图
new_g = tf.Graph()
print("创建的图:", new_g)

# 在自定义图中添加数据
with new_g.as_default():
    new_a = tf.constant(10.0)
    new_b = tf.constant(20.0)
    new_c = tf.add(new_a, new_b)

print(new_a)
print(new_b)
print(new_c)
print('*' * 50)

# 开启会话，如果不指定，默认是在默认图上进行
with tf.compat.v1.Session() as sess:
    # 将事件文件写入指定目录
    # 注意：大多数windows电脑必须用localhost才能访问
    file_writer = tf.compat.v1.summary.FileWriter(
        r"F:\virtual_environment\AI_Study\AI_study_code\人工智能物体检测和分割\tmp\summary",
        graph=sess.graph)
    a = sess.run(a)
    b = sess.run(b)
    c = sess.run(c)
    print("开启默认图的会话")
    print(a)
    print(b)
    print(c)
    print('*' * 50)

# 开启创建图的会话
with tf.compat.v1.Session(graph=new_g) as sess:
    new_a = sess.run(new_a)
    new_b = sess.run(new_b)
    new_c = sess.run(new_c)
    print("开启创建图的会话")
    print(new_a)
    print(new_b)
    print(new_c)
